 <template>
    <div class="order_page">
        <head-top head-title="订单列表" go-back='true'></head-top>
        <ul class="order_list_ul">
            <li class="order_list_li" v-for="item in orderList" :key="item.id">

             <section class="license_container" v-if="showlicenseImg" @click="showlicenseImg = false">
                 <img :src="erweima">
             </section>

                <img src="https://www.jiangsuduotianfangzhikeji.cn/uploads/20231202/fc86afce514f87edca5899270ce1fd67.png" class="restaurant_image">
                <div class="order_item_right">
                    <div @click="showDetail(item)">
                        <header class="order_item_right_header">
                            <div class="order_header">
                                <h4 >
                                    <span class="ellipsis">多田餐厅 </span>
                                    <svg fill="#333" class="arrow_right">
                                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#arrow-right"></use>
                                    </svg>
                                </h4>
                                <p class="order_time">{{item.ordertime_text}}</p>
                            </div>
                            <p class="order_status">
                                {{
                                    item.status == 1 ? '支付完成' :
                                    item.status == 2 ? '已出餐' :
                                    item.status == 3 ? '退款中' :
                                    '已退款'
                                }}
                            </p>

                        </header>
                        <div class="order_basket">
                            <p class="order_name ellipsis">{{item.description}}{{item.order_detail.length > 1 ? ' 等' + item.order_detail.length + '件商品' : ''}}</p>
                            <p class="order_amount">¥{{item.total_price}}</p>

                        </div>
                        <button v-if="item.status == 1 || item.status == 0" @click.stop="submit(item.order_id)">申请退款</button>
                        <button @click.stop="showLicenseImg(item.erweima)" v-if="item.erweima">查看二维码</button>
                    </div>
                    <div class="order_again">
                        <div v-if="item.status == 0" class="payBtn">去支付 ></div>
                        <!-- <compute-time v-if="item.status_bar.title == '等待支付'" :time="item.time_pass"></compute-time> -->
                        <!-- <router-link :to="{path: '/shop', query: {geohash, id: item.restaurant_id}}" tag="span" class="buy_again" v-else>再来一单</router-link> -->
                    </div>
                </div>
            </li>
        </ul>
        <!-- <foot-guide></foot-guide> -->
        <transition name="loading">
            <loading v-show="showLoading"></loading>
        </transition>
        <transition name="router-slid" mode="out-in">
            <router-view></router-view>
        </transition>
        <alert-tip
            v-if="showAlert"
            :type="alertType"
            @backMoneyReason="backMoneyReason"
            @closeTip="close"
            @orderPage="showAlert = false"
            :alertText="alertText"></alert-tip>
    </div>
</template>

<script>
    import {mapState, mapMutations} from 'vuex'
    import headTop from 'src/components/header/head'
    import computeTime from 'src/components/common/computeTime'
    import loading from 'src/components/common/loading'
    import {getImgPath} from 'src/components/common/mixin'
    import footGuide from 'src/components/footer/footGuide'
    import {getOrderList, getMyOrderList, userBackMoney} from 'src/service/getData'
    import {loadMore} from 'src/components/common/mixin'
    import {imgBaseUrl, myImgBaseUrl} from 'src/config/env'
    import alertTip from 'src/components/common/alertTip'

    export default {
      data(){
            return{
                orderList: null, //订单列表
                showlicenseImg: false,
                erweima: "",
                offset: 0,
                preventRepeat: false,  //防止重复获取
                showLoading: true, //显示加载动画
                imgBaseUrl,
                myImgBaseUrl,
                showAlert:false,
                alertText: null,
                alertType: 1,
                currentId: null
            }
        },
        mounted(){
            this.initData();
            console.log(this.timestampToTime(new Date().getTime()/1000))
        },
        components: {
            headTop,
            footGuide,
            loading,
            computeTime,
            alertTip
        },
        computed: {
            ...mapState([
                'userInfo', 'geohash'
            ]),
        },
        methods: {
             ...mapMutations([
               'SAVE_ORDER'
            ]),
            backMoneyReason(value) {
                this.showAlert = false
                this.alertType = null
                userBackMoney(this.currentId, value).then(res => {
                    console.log(res)
                    this.showAlert = true
                    this.alertText = res.data.msg
                    this.initData()
                })
            },
            showLicenseImg(img){
                this.showlicenseImg = true;
                this.erweima = img;
            },
            submit(id) {
                this.alertType = 1
                this.alertText = '确认申请退款'
                this.showAlert = true
                this.currentId = id
            },
            close() {
                this.showAlert = false;
                this.initData()
            },
            timestampToTime(timestamp) {
                var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
                var Y = date.getFullYear() + '-';
                var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
                var D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
                var h = (date.getHours()+1 < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
                var m = (date.getMinutes()+1 < 10 ? '0'+(date.getMinutes()+1) : date.getMinutes()+1);
                var s = ':' + (date.getSeconds()+1 < 10 ? '0'+(date.getSeconds()+1) : date.getSeconds()+1);
                return Y+M+D+h+m;
            },
            //初始化获取信息
            async initData(){
                getMyOrderList().then(res => {
                    this.hideLoading();
                    this.orderList = res.data
                })
                // if (this.userInfo && this.userInfo.user_id) {
                //     let res = await getOrderList(this.userInfo.user_id, this.offset);
                //     this.orderList = [...res];
                //     this.hideLoading();
                // }else{
                //     this.hideLoading();
                // }
            },
            //加载更多
            // async loaderMore(){
            //     if (this.preventRepeat) {
            //         return
            //     }
            //     this.preventRepeat = true;
            //     this.showLoading = true;
            //     this.offset += 10;
            //     //获取信息
            //     let res = await getOrderList(this.userInfo.user_id, this.offset);
            //     this.orderList = [...this.orderList, ...res];
            //     this.hideLoading();
            //     if (res.length < 10) {
            //         return
            //     }
            //     this.preventRepeat = false;
            // },
            //显示详情页
            showDetail(item){
                this.SAVE_ORDER(item);
                this.preventRepeat = false;
                this.$router.push({
                    path: '/orderDetail',
                    query: {
                        order_id: item.order_id
                    }
                });
            },
            //生产环境与发布环境隐藏loading方式不同
            hideLoading(){
                this.showLoading = false;
            },
        },
        // watch: {
        //     userInfo: function (value) {
        //         if (value && value.user_id && !this.orderList) {
        //             this.initData();
        //         }
        //     }
        // }
    }
</script>

<style lang="scss" scoped>
    @import 'src/style/mixin';

    .order_page{
        background-color: #f1f1f1;
        margin-bottom: 1.95rem;
        p, span, h4{
            font-family: Helvetica Neue,Tahoma,Arial;
        }
    }
    .license_container{
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(0,0,0,0.1);
        z-index: 101;
        img{
            width: 100%;
            @include center;
        }
    }
    .order_list_ul{
        padding-top: 1.95rem;
        .order_list_li{
            background-color: #fff;
            display: flex;
            margin-bottom: 0.5rem;
            padding: .6rem .6rem 0;
            .restaurant_image{
                @include wh(2rem, 2rem);
                margin-right: 0.4rem;
            }
            .order_item_right{
                flex: 5;
                .order_item_right_header{
                    border-bottom: 0.025rem solid #f5f5f5;
                    padding-bottom: .3rem;
                    @include fj;
                    .order_header{
                        h4{
                            display: flex;
                            align-items: center;
                            justify-content: flex-start;
                            @include sc(.75rem, #333);
                            line-height: 1rem;
                            width: 9rem;

                            .arrow_right{
                                @include wh(.4rem, .4rem);
                                fill: #ccc;
                                margin-right: .2rem;
                            }
                        }
                        .order_time{
                            @include sc(.55rem, #999);
                            line-height: .8rem;
                        }
                    }
                    .order_status{
                        @include sc(.6rem, #333);
                    }
                }
                button{
                    padding: 3px 10px;
                    border-radius: 5px;
                    font-size: 0.6rem;
                }
                .order_basket{
                    @include fj;
                    line-height: 2rem;
                    border-bottom: 0.025rem solid #f5f5f5;
                    .order_name{
                        @include sc(.6rem, #666);
                        width: 10rem;
                    }
                    .order_amount{
                        @include sc(.6rem, #f60);
                        font-weight: bold;
                    }
                }
                .order_again{
                    text-align: right;
                    line-height: 1.6rem;
                    display: flex;
                    align-items: center;
                    justify-content: flex-end;
                    padding: 0.2rem;
                    box-sizing: border-box;
                    .payBtn{
                        font-size: 0.6rem;
                    }
                    .buy_again{
                        @include sc(.55rem, #3190e8);
                        border: 0.025rem solid #3190e8;
                        padding: .1rem .2rem;
                        border-radius: .15rem;
                    }
                }
            }
        }
    }
    .loading-enter-active, .loading-leave-active {
        transition: opacity .7s
    }
    .loading-enter, .loading-leave-active {
        opacity: 0
    }
    .router-slid-enter-active, .router-slid-leave-active {
        transition: all .4s;
    }
    .router-slid-enter, .router-slid-leave-active {
        transform: translate3d(2rem, 0, 0);
        opacity: 0;
    }
</style>
